home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / man / lib / tcl / Hash.man < prev    next >
Encoding:
Text File  |  1992-08-07  |  11.8 KB  |  387 lines

  1. '\"
  2. '\" Copyright 1989 Regents of the University of California
  3. '\" Permission to use, copy, modify, and distribute this
  4. '\" documentation for any purpose and without fee is hereby
  5. '\" granted, provided that this notice appears in all copies.
  6. '\" The University of California makes no representations about
  7. '\" the suitability of this material for any purpose.  It is
  8. '\" provided "as is" without express or implied warranty.
  9. '\" 
  10. '\" $Header: /user6/ouster/tcl/man/RCS/Hash.man,v 1.4 91/12/06 10:34:55 ouster Exp $ SPRITE (Berkeley)
  11. '\" 
  12. .\" The definitions below are for supplemental macros used in Sprite
  13. .\" manual entries.
  14. .\"
  15. .\" .HS name section [date [version]]
  16. .\"    Replacement for .TH in other man pages.  See below for valid
  17. .\"    section names.
  18. .\"
  19. .\" .AP type name in/out [indent]
  20. .\"    Start paragraph describing an argument to a library procedure.
  21. .\"    type is type of argument (int, etc.), in/out is either "in", "out",
  22. .\"    or "in/out" to describe whether procedure reads or modifies arg,
  23. .\"    and indent is equivalent to second arg of .IP (shouldn't ever be
  24. .\"    needed;  use .AS below instead)
  25. .\"
  26. .\" .AS [type [name]]
  27. .\"    Give maximum sizes of arguments for setting tab stops.  Type and
  28. .\"    name are examples of largest possible arguments that will be passed
  29. .\"    to .AP later.  If args are omitted, default tab stops are used.
  30. .\"
  31. .\" .BS
  32. .\"    Start box enclosure.  From here until next .BE, everything will be
  33. .\"    enclosed in one large box.
  34. .\"
  35. .\" .BE
  36. .\"    End of box enclosure.
  37. .\"
  38. .\" .VS
  39. .\"    Begin vertical sidebar, for use in marking newly-changed parts
  40. .\"    of man pages.
  41. .\"
  42. .\" .VE
  43. .\"    End of vertical sidebar.
  44. .\"
  45. .\" .DS
  46. .\"    Begin an indented unfilled display.
  47. .\"
  48. .\" .DE
  49. .\"    End of indented unfilled display.
  50. .\"
  51. '\"    # Heading for Sprite man pages
  52. .de HS
  53. .if '\\$2'cmds'       .TH \\$1 1 \\$3 \\$4
  54. .if '\\$2'lib'        .TH \\$1 3 \\$3 \\$4
  55. .if '\\$2'tcl'        .TH \\$1 3 \\$3 \\$4
  56. .if '\\$2'tk'         .TH \\$1 3 \\$3 \\$4
  57. .if t .wh -1.3i ^B
  58. .nr ^l \\n(.l
  59. .ad b
  60. ..
  61. '\"    # Start an argument description
  62. .de AP
  63. .ie !"\\$4"" .TP \\$4
  64. .el \{\
  65. .   ie !"\\$2"" .TP \\n()Cu
  66. .   el          .TP 15
  67. .\}
  68. .ie !"\\$3"" \{\
  69. .ta \\n()Au \\n()Bu
  70. \&\\$1    \\fI\\$2\\fP    (\\$3)
  71. .\".b
  72. .\}
  73. .el \{\
  74. .br
  75. .ie !"\\$2"" \{\
  76. \&\\$1    \\fI\\$2\\fP
  77. .\}
  78. .el \{\
  79. \&\\fI\\$1\\fP
  80. .\}
  81. .\}
  82. ..
  83. '\"    # define tabbing values for .AP
  84. .de AS
  85. .nr )A 10n
  86. .if !"\\$1"" .nr )A \\w'\\$1'u+3n
  87. .nr )B \\n()Au+15n
  88. .\"
  89. .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
  90. .nr )C \\n()Bu+\\w'(in/out)'u+2n
  91. ..
  92. '\"    # BS - start boxed text
  93. '\"    # ^y = starting y location
  94. '\"    # ^b = 1
  95. .de BS
  96. .br
  97. .mk ^y
  98. .nr ^b 1u
  99. .if n .nf
  100. .if n .ti 0
  101. .if n \l'\\n(.lu\(ul'
  102. .if n .fi
  103. ..
  104. '\"    # BE - end boxed text (draw box now)
  105. .de BE
  106. .nf
  107. .ti 0
  108. .mk ^t
  109. .ie n \l'\\n(^lu\(ul'
  110. .el \{\
  111. .\"    Draw four-sided box normally, but don't draw top of
  112. .\"    box if the box started on an earlier page.
  113. .ie !\\n(^b-1 \{\
  114. \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  115. .\}
  116. .el \}\
  117. \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  118. .\}
  119. .\}
  120. .fi
  121. .br
  122. .nr ^b 0
  123. ..
  124. '\"    # VS - start vertical sidebar
  125. '\"    # ^Y = starting y location
  126. '\"    # ^v = 1 (for troff;  for nroff this doesn't matter)
  127. .de VS
  128. .mk ^Y
  129. .ie n 'mc \s12\(br\s0
  130. .el .nr ^v 1u
  131. ..
  132. '\"    # VE - end of vertical sidebar
  133. .de VE
  134. .ie n 'mc
  135. .el \{\
  136. .ev 2
  137. .nf
  138. .ti 0
  139. .mk ^t
  140. \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
  141. .sp -1
  142. .fi
  143. .ev
  144. .\}
  145. .nr ^v 0
  146. ..
  147. '\"    # Special macro to handle page bottom:  finish off current
  148. '\"    # box/sidebar if in box/sidebar mode, then invoked standard
  149. '\"    # page bottom macro.
  150. .de ^B
  151. .ev 2
  152. 'ti 0
  153. 'nf
  154. .mk ^t
  155. .if \\n(^b \{\
  156. .\"    Draw three-sided box if this is the box's first page,
  157. .\"    draw two sides but no top otherwise.
  158. .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  159. .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  160. .\}
  161. .if \\n(^v \{\
  162. .nr ^x \\n(^tu+1v-\\n(^Yu
  163. \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
  164. .\}
  165. .bp
  166. 'fi
  167. .ev
  168. .if \\n(^b \{\
  169. .mk ^y
  170. .nr ^b 2
  171. .\}
  172. .if \\n(^v \{\
  173. .mk ^Y
  174. .\}
  175. ..
  176. '\"    # DS - begin display
  177. .de DS
  178. .RS
  179. .nf
  180. .sp
  181. ..
  182. '\"    # DE - end display
  183. .de DE
  184. .fi
  185. .RE
  186. .sp .5
  187. ..
  188. .HS Tcl_Hash tcl
  189. .BS
  190. .SH NAME
  191. .na
  192. Tcl_InitHashTable, Tcl_DeleteHashTable, Tcl_CreateHashEntry, Tcl_DeleteHashEntry, Tcl_FindHashEntry, Tcl_GetHashValue, Tcl_SetHashValue, Tcl_GetHashKey, Tcl_FirstHashEntry, Tcl_NextHashEntry, Tcl_HashStats \- procedures to manage hash tables
  193. .SH SYNOPSIS
  194. .nf
  195. \fB#include <tclHash.h>\fR
  196. .sp
  197. \fBTcl_InitHashTable\fR(\fItablePtr, keyType\fR)
  198. .sp
  199. \fBTcl_DeleteHashTable\fR(\fItablePtr\fR)
  200. .sp
  201. Tcl_HashEntry *
  202. \fBTcl_CreateHashEntry\fR(\fItablePtr, key, newPtr\fR)
  203. .sp
  204. \fBTcl_DeleteHashEntry\fR(\fIentryPtr\fR)
  205. .sp
  206. Tcl_HashEntry *
  207. \fBTcl_FindHashEntry\fR(\fItablePtr, key\fR)
  208. .sp
  209. ClientData
  210. \fBTcl_GetHashValue\fR(\fIentryPtr\fR)
  211. .sp
  212. \fBTcl_SetHashValue\fR(\fIentryPtr, value\fR)
  213. .sp
  214. char *
  215. \fBTcl_GetHashKey\fR(\fItablePtr, entryPtr\fR)
  216. .sp
  217. Tcl_HashEntry *
  218. \fBTcl_FirstHashEntry\fR(\fItablePtr, searchPtr\fR)
  219. .sp
  220. Tcl_HashEntry *
  221. \fBTcl_NextHashEntry\fR(\fIsearchPtr\fR)
  222. .sp
  223. char *
  224. \fBTcl_HashStats\fR(\fItablePtr\fR)
  225. .SH ARGUMENTS
  226. .AS Tcl_HashSearch *searchPtr
  227. .AP Tcl_HashTable *tablePtr in
  228. Address of hash table structure (for all procedures but
  229. \fBTcl_InitHashTable\fR, this must have been initialized by
  230. previous call to \fBTcl_InitHashTable\fR).
  231. .AP int keyType in
  232. Kind of keys to use for new hash table.  Must be either
  233. TCL_STRING_KEYS, TCL_ONE_WORD_KEYS, or an integer value
  234. greater than 1.
  235. .AP char *key in
  236. Key to use for probe into table.  Exact form depends on
  237. \fIkeyType\fR used to create table.
  238. .AP int *newPtr out
  239. The word at \fI*newPtr\fR is set to 1 if a new entry was created
  240. and 0 if there was already an entry for \fIkey\fR.
  241. .AP Tcl_HashEntry *entryPtr in
  242. Pointer to hash table entry.
  243. .AP ClientData value in
  244. New value to assign to hash table entry.  Need not have type
  245. ClientData, but must fit in same space as ClientData.
  246. .AP Tcl_HashSearch *searchPtr in
  247. Pointer to record to use to keep track of progress in enumerating
  248. all the entries in a hash table.
  249. .BE
  250.  
  251. .SH DESCRIPTION
  252. .PP
  253. A hash table consists of zero or more entries, each consisting of
  254. a key and a value.
  255. Given the key for an entry, the hashing routines can very quickly
  256. locate the entry, and hence its value.
  257. There may be at most one entry in a hash table with a
  258. particular key, but many entries may have the same value.
  259. Keys can take one of three forms:  strings,
  260. one-word values, or integer arrays.
  261. All of the keys in a given table have the same form, which is
  262. specified when the table is initialized.
  263. .PP
  264. The value of a hash table entry can be anything that fits in
  265. the same space as a ``char *'' pointer.
  266. Values for hash table entries are managed entirely by clients,
  267. not by the hash module itself.
  268. Typically each entry's value is a pointer to a data structure
  269. managed by client code.
  270. .PP
  271. Hash tables grow gracefully as the number of entries increases,
  272. so that there are always less than three entries per hash bucket,
  273. on average.
  274. This allows for fast lookups regardless of the number of entries
  275. in a table.
  276. .PP
  277. \fBTcl_InitHashTable\fR initializes a structure that describes
  278. a new hash table.
  279. The space for the structure is provided by the caller, not by
  280. the hash module.
  281. The value of \fIkeyType\fR indicates what kinds of keys will
  282. be used for all entries in the table.  \fIKeyType\fR must have
  283. one of the following values:
  284. .IP \fBTCL_STRING_KEYS\fR 25
  285. Keys are null-terminated ASCII strings.
  286. They are passed to hashing routines using the address of the
  287. first character of the string.
  288. .IP \fBTCL_ONE_WORD_KEYS\fR 25
  289. Keys are single-word values;  they are passed to hashing routines
  290. and stored in hash table entries as ``char *'' values.
  291. The pointer value is the key;  it need not (and usually doesn't)
  292. actually point to a string.
  293. .IP \fIother\fR 25
  294. If \fIkeyType\fR is not TCL_STRING_KEYS or TCL_ONE_WORD_KEYS,
  295. then it must be an integer value greater than 1.
  296. In this case the keys will be arrays of ``int'' values, where
  297. \fIkeyType\fR gives the number of ints in each key.
  298. This allows structures to be used as keys.
  299. All keys must have the same size.
  300. Array keys are passed into hashing functions using the address
  301. of the first int in the array.
  302. .PP
  303. \fBTcl_DeleteHashTable\fR deletes all of the entries in a hash
  304. table and frees up the memory associated with the table's
  305. bucket array and entries.
  306. It does not free the actual table structure (pointed to
  307. by \fItablePtr\fR), since that memory is assumed to be managed
  308. by the client.
  309. \fBTcl_DeleteHashTable\fR also does not free or otherwise
  310. manipulate the values of the hash table entries.
  311. If the entry values point to dynamically-allocated memory, then
  312. it is the client's responsibility to free these structures
  313. before deleting the table.
  314. .PP
  315. \fBTcl_CreateHashEntry\fR locates the entry corresponding to a
  316. particular key, creating a new entry in the table if there
  317. wasn't already one with the given key.
  318. If an entry already existed with the given key then \fI*newPtr\fR
  319. is set to zero.
  320. If a new entry was created, then \fI*newPtr\fR is set to a non-zero
  321. value and the value of the new entry will be set to zero.
  322. The return value from \fBTcl_CreateHashEntry\fR is a pointer to
  323. the entry, which may be used to retrieve and modify the entry's
  324. value or to delete the entry from the table.
  325. .PP
  326. \fBTcl_DeleteHashEntry\fR will remove an existing entry from a
  327. table.
  328. The memory associated with the entry itself will be freed, but
  329. the client is responsible for any cleanup associated with the
  330. entry's value, such as freeing a structure that it points to.
  331. .PP
  332. \fBTcl_FindHashEntry\fR is similar to \fBTcl_CreateHashEntry\fR
  333. except that it doesn't create a new entry if the key doesn't exist;
  334. instead, it returns NULL as result.
  335. .PP
  336. \fBTcl_GetHashValue\fR and \fBTcl_SetHashValue\fR are used to
  337. read and write an entry's value, respectively.
  338. Values are stored and retrieved as type ``ClientData'', which is
  339. large enough to hold a pointer value.  On almost all machines this is
  340. large enough to hold an integer value too.
  341. .PP
  342. \fBTcl_GetHashKey\fR returns the key for a given hash table entry,
  343. either as a pointer to a string, a one-word (``char *'') key, or
  344. as a pointer to the first word of an array of integers, depending
  345. on the \fIkeyType\fR used to create a hash table.
  346. In all cases \fBTcl_GetHashKey\fR returns a result with type
  347. ``char *''.
  348. When the key is a string or array, the result of \fBTcl_GetHashKey\fR
  349. points to information in the table entry;  this information will
  350. remain valid until the entry is deleted or its table is deleted.
  351. .PP
  352. \fBTcl_FirstHashEntry\fR and \fBTcl_NextHashEntry\fR may be used
  353. to scan all of the entries in a hash table.
  354. A structure of type ``Tcl_HashSearch'', provided by the client,
  355. is used to keep track of progress through the table.
  356. \fBTcl_FirstHashEntry\fR initializes the search record and
  357. returns the first entry in the table (or NULL if the table is
  358. empty).
  359. Each susequent call to \fBTcl_NextHashEntry\fR returns the
  360. next entry in the table or
  361. NULL if the end of the table has been reached.
  362. A call to \fBTcl_FirstHashEntry\fR followed by calls to
  363. \fBTcl_NextHashEntry\fR will return each of the entries in
  364. the table exactly once, in an arbitrary order.
  365. It is unadvisable to modify the structure of the table, e.g.
  366. by creating or deleting entries, while the search is in
  367. progress.
  368. .PP
  369. \fBTcl_HashStats\fR returns a dynamically-allocated string with
  370. overall information about a hash table, such as the number of
  371. entries it contains, the number of buckets in its hash array,
  372. and the utilization of the buckets.
  373. It is the caller's responsibility to free the result string
  374. by passing it to \fBfree\fR.
  375. .PP
  376. The header file \fBtclHash.h\fR defines the actual data structures
  377. used to implement hash tables.
  378. This is necessary so that clients can allocate Tcl_HashTable
  379. structures and so that macros can be used to read and write
  380. the values of entries.
  381. However, users of the hashing routines should never refer directly
  382. to any of the fields of any of the hash-related data structures;
  383. use the procedures and macros defined here.
  384.  
  385. .SH KEYWORDS
  386. hash table, key, lookup, search, value
  387.